import * as types from './mutation-types'
import km from '../../api/km'
import { Message } from 'element-ui'
import { m7Language } from '@/utils/m7Utils.js'
import * as gtypes from '../../mutation-types'
// import {getCache} from '../../actions'
// 目录树查询
export const queryCatalogList = ({commit, state, rootState, dispatch}, data) => {
  // 如果时webchat，webchatTree字段保存
  let type = data.type
  delete data.type
  return km.queryCatalogList(data).then(
    rep => {
      if (rep.success) {
        let tree = treeDataLimit(data.kmType, rep.tree)
        commit(types.QUERY_CATALOG_LIST, {tree, type})
        return tree
      }
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 最近更新查询
export const queryRecentList = ({commit}, data) => {
  return km.queryRecentList(data).then(
      rep => {
        if (rep.success) {
          commit(types.QUERY_RECENT_LIST, rep.list)
        }
        return rep
      }
    ).catch(() => {
      // 'message.default_tips'
    })
}
// 知识点查询
export const queryItemList = ({commit}, data) => {
  return km.queryItemList(data).then(
      rep => {
        if (rep.success) {
          data.type !== 'im' && commit(types.QUERY_ITEM_LIST, rep)
          rep.message === 'no permission' && Message.error(m7Language('km.operateDeptTips'))
        }
        return rep
      }
    ).catch(() => {
      // 'message.default_tips'
    })
}
// 添加目录
export const addCatalog = ({commit}, data) => {
  return km.addCatalog(data).then(
    rep => {
      if (rep.success) {
        commit(types.ADD_CATALOG, rep)
      } else {
        if (rep.code === '1001') {
          commit(gtypes.SET_ERROR, 'message.alreadyExists')
          return
        }
      }
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 删除目录
export const delCatalog = ({commit}, data) => {
  return km.delCatalog(data).then(
    rep => {
      console.log(rep)
      if (rep.success) {
        commit(types.DEL_CATALOG, rep.success)
        commit(gtypes.SET_SUCCESS, 'message.deleteSuccess')
      }
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 更新目录
export const updateCatalog = ({commit}, data) => {
  return km.updateCatalog(data).then(
    rep => {
      console.log(rep)
      if (rep.success) {
        commit(types.UPDATE_CATALOG, rep)
      } else {
        if (rep.code === '1001') {
          commit(gtypes.SET_ERROR, 'message.alreadyxists')
        } else {
          commit(gtypes.SET_ERROR, 'message.default_tips')
        }
      }
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 打开知识点编辑页面
export const showItemEdit = ({commit}, data) => {
  return km.showItemEdit(data).then(
    rep => {
      if (rep.success) {
        commit(types.SHOW_ITEM_EDIT, rep)
      } else {
        commit(gtypes.SET_ERROR, 'message.notExists')
      }
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 添加知识点
export const saveItem = ({commit}, data) => {
  return km.saveItem(data).then(
    rep => {
      console.log(rep)
      if (rep.success) {
        commit(types.SAVE_ITEM, rep)
      }
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 编辑知识点
export const updateItem = ({commit}, data) => {
  return km.updateItem(data).then(
    rep => {
      console.log(rep)
      if (rep.success) {
        commit(types.UPDATE_ITEM, rep)
        commit(gtypes.SET_SUCCESS, 'userInfo.editSuccess')
      }
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 知识点删除
export const delItem = ({commit}, data) => {
  return km.delItem(data).then(
    rep => {
      // console.log(rep)
      // if (rep.success) {
      //   commit(types.DEL_ITEM, rep)
      // }
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 删除知识点中附件
export const delAttach = ({commit}, data) => {
  return km.delAttach(data).then(
    rep => {
      console.log(rep)
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 知识点置顶
export const kmPlaceTop = ({commit}, data) => {
  return km.kmPlaceTop(data).then(
    rep => {
      // console.log(rep)
      // if (rep.success) {
      //   commit(types.DEL_ITEM, rep)
      // }
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 查询知识库字典
export const queryKmDic = ({commit}, data) => {
  return km.queryKmDic(data).then(
    rep => {
      if (rep.success) {
        commit(types.QUERY_KMDIC, rep)
      }
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 查询知识库
export const queryWholeKmDic = ({commit}, data) => {
  return km.queryWholeKmDic(data).then(
    rep => {
      console.log(rep)
      if (rep.success) {
        commit(types.QUERY_WHOLE_KMDIC, rep)
      } else {
        commit(gtypes.SET_ERROR, 'message.getDataFail')
      }
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 查询知识库热词
export const queryKmHotWords = ({commit}, data) => {
  return km.queryKmHotWords(data).then(
    rep => {
      console.log(rep)
      if (rep.success) {
        rep.list.forEach((item) => {
          item.value = item.key
        })
        commit(types.QUERY_KM_HOTWORDS, rep)
      }
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 解析上传文件
export const uploadDoc = ({commit}, data) => {
  return km.uploadDoc(data).then(
    rep => {
      if (rep.success) {
        commit(types.UPLOAD_DOC, rep)
      } else if (rep.Message) {
        commit(gtypes.SET_ERROR, rep.Message)
      } else {
        commit(gtypes.SET_ERROR, 'message.transformFail')
      }
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 提交新评论
export const submitComments = ({commit}, data) => {
  return km.submitComments(data).then(
    rep => {
      if (rep.success) {
        return rep
      } else if (rep.Message) {
        commit(gtypes.SET_ERROR, rep.Message)
      } else {
        commit(gtypes.SET_ERROR, 'message.transformFail')
      }
    }
  ).catch(() => {
      // 'message.default_tips'
  })
}
// 获取知识库日志
export const getKmLogs = ({commit}, data) => {
  return km.getKmLogs(data).then(
    rep => {
      if (rep.success) {
        return rep
      } else {
        commit(gtypes.SET_ERROR, 'message.default_tips')
      }
    }
  ).catch(() => {
      // 'message.default_tips'
  })
}
// 获取新评论
export const getComments = ({commit}, data) => {
  return km.getComments(data).then(
    rep => {
      if (rep.success) {
        return rep
      } else if (rep.Message) {
        commit(gtypes.SET_ERROR, rep.Message)
      } else {
        commit(gtypes.SET_ERROR, 'message.transformFail')
      }
    }
  ).catch(() => {
      // 'message.default_tips'
  })
}
// 删除评论
export const deleteTheComments = ({commit}, data) => {
  return km.deleteTheComments(data).then(
    rep => {
      if (rep.success) {
        return rep
      }
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 知识库导入校验
export const getXlsHeadsKm = ({commit}, data) => {
  return km.getXlsHeads(data).then(
    rep => {
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 知识库导出
export const addKmExportTask = ({commit}, data) => {
  return km.addKmExportTask(data).then(
    rep => {
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 知识库导出列表
export const getKmExportList = ({commit}, data) => {
  return km.getKmExportList(data).then(
    rep => {
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 知识库导入
export const importKmItem = ({commit}, data) => {
  return km.importKmItem(data).then(
    rep => {
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 知识库导入列表
export const getKmImportList = ({commit}, data) => {
  return km.getKmImportList(data).then(
    rep => {
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 知识库导入列表删除
export const removeKmImport = ({commit}, data) => {
  return km.removeKmImport(data).then(
    rep => {
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 知识库树，前端校验，内部3级，外部1级
function treeDataLimit (type, childs) {
  if (type === 'inner') {
    for (var i = 0; i < childs.length; i++) {
      var item = childs[i]
      if (item.childs && item.childs.length !== 0) {
        for (var m = 0; m < item.childs.length; m++) {
          var item1 = item.childs[m]
          if (item1.childs && item1.childs.length !== 0) {
            for (var n = 0; n < item1.childs.length; n++) {
              childs[i].childs[m].childs[n].childs = []
              // oparateBtn为true，则添加按钮隐藏
              childs[i].childs[m].childs[n].oparateBtn = true
            }
          }
        }
      }
    }
  }
  if (type === 'outer') {
    for (let i = 0; i < childs.length; i++) {
      let item = childs[i]
      if (item.childs.length !== 0) {
        childs[i].childs = []
      }
      childs[i].oparateBtn = true
    }
  }
  return childs
}
// 批量知识点删除
export const batchDelItem = ({commit}, data) => {
  return km.batchDelItem(data).then(
    rep => {
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 批量更新目录
export const batchUpdateCid = ({commit}, data) => {
  return km.batchUpdateCid(data).then(
    rep => {
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
// 批量更新有效期
export const batchUpdateValidityPeriod = ({commit}, data) => {
  return km.batchUpdateValidityPeriod(data).then(
    rep => {
      return rep
    }
  ).catch(() => {
    // 'message.default_tips'
  })
}
